import {createInertiaApp, Head, Link} from '@inertiajs/vue3'
import createServer from '@inertiajs/vue3/server'
import {renderToString} from '@vue/server-renderer'
import {createSSRApp, h} from 'vue'
import {resolvePageComponent} from "laravel-vite-plugin/inertia-helpers";
import {ZiggyVue} from 'ziggy-js';

const appName =  '人称外号大脸猫';
createServer(page =>
        createInertiaApp({
            page,
            render: renderToString,
            title: (title) => `${title} - ${appName}`,
            resolve: resolvePage,
            setup({ App, props, plugin }) {
                createSSRApp({
                    render: () => h(App, props),
                }).use(plugin)
                    .component('Head', Head) // 注册 Head 组件
                    .component('Link', Link) // 注册 Link 组件
                    .use(ZiggyVue)
            },
        }),
    { cluster: true },
)
function resolvePage(name) {
    const pages = import.meta.glob('../pages/web/**/*.vue');
    if (pages[`../pages/web/${name}.vue`]){
        return resolvePageComponent(`../pages/web/${name}.vue`, pages);
    }
    // 添加默认返回或错误处理
    throw new Error(`Page not found: ${name}`);
}
